//
//  NineViewController.m
//  项目二
//
//  Created by rimi on 16/5/13.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "NineViewController.h"

@interface NineViewController ()

@end

@implementation NineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"滑条、分段控制器";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUISlider];
    [self initUISegmentedControl];
}

- (void)initUISlider{
    //滑条
    UISlider *slider = [[UISlider alloc] init];
    slider.bounds = CGRectMake(0, 0, 200, 30);
    slider.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) - 150);
    //设置最小值
    slider.minimumValue = 0.1;
    //设置最大值
    slider.maximumValue = 1.0;
    //设置当前值
    slider.value = 1.0;
    //设置最大值的颜色
    slider.maximumTrackTintColor = [UIColor greenColor];
    //设置最小值的颜色
    slider.minimumTrackTintColor = [UIColor redColor];
    
    [slider addTarget:self action:@selector(controlProgress:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
}
- (void)initUISegmentedControl{
    //分段控制器
    UISegmentedControl *sengmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"白色", @"蓝色", @"红色"]];
    sengmentedControl.bounds = CGRectMake(0, 0, 250, 30);
    sengmentedControl.center = self.view.center;
    //设置当前选中项
    sengmentedControl.selectedSegmentIndex = 0;
    //设置字体颜色
    sengmentedControl.tintColor = [UIColor redColor];
    
    [sengmentedControl addTarget:self action:@selector(controlProgress:) forControlEvents:UIControlEventValueChanged];
    //设置字体颜色
    [sengmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
    [self.view addSubview:sengmentedControl];

}
#pragma mark - 点击事件

-(void)controlProgress:(UIView *)sender{
    if ([sender isKindOfClass:[UISlider class]]) {
        UISlider *slider = (UISlider *)sender;
        //改变背景视图的透明度
        self.view.alpha = slider.value;
    }
    
    if ([sender isKindOfClass:[UISegmentedControl class]]) {
        UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
        NSArray *colors = @[[UIColor whiteColor], [UIColor blueColor], [UIColor redColor]];
        self.view.backgroundColor = colors[segmentedControl.selectedSegmentIndex];
    }

}
@end
